import { Controller, Get, Request, HttpCode, Header, Post, Body, Res, HttpStatus, HttpException, UseFilters, ForbiddenException, BadRequestException } from '@nestjs/common';
import { Response } from 'express';
import { HttpExceptionFilter } from '../filter/http-exception.filter';

export class CreateCatDto {
  readonly name: string;
  readonly age: number;
  readonly breed: string;
}

@Controller('dog')
export class DogController {
  // 匹配上面没有匹配成功的路由
  @Get()
  // @UseFilters(new HttpExceptionFilter())
  findIndex() {
    // throw new HttpException({
    //   status: HttpStatus.FORBIDDEN,
    //   error: 'This is a custom message',
    // }, HttpStatus.FORBIDDEN);
    // 没有权限访问,注册了全局的过滤器
    throw new BadRequestException();
  }

  @Get('eat')
  findEat() {
    return 'dog eat';
  }

  @Get('run*')
  @HttpCode(204)
  @Header('Cache-Control', 'none')
  findRun(
    @Request() req: any
  ) {
    console.log(req.url);
    return 'dog run';
  }

  @Post('list')
  create(@Body() createCatDto: CreateCatDto) {
    return 'This action adds a new cat';
  }

  @Get('page')
  findPage(@Res() res: Response) {
    // console.log(res, HttpStatus);
    // return 'page';

    // 返回code和data
    res.status(HttpStatus.PAYLOAD_TOO_LARGE).json({ user: 'tj' });
  }

  // 匹配上面没有匹配成功的路由
  @Get('*')
  // @UseFilters(new HttpExceptionFilter())
  findAll() {
    // throw new HttpException({
    //   status: HttpStatus.FORBIDDEN,
    //   error: 'This is a custom message',
    // }, HttpStatus.FORBIDDEN);
    // 没有权限访问
    throw new ForbiddenException();
  }
}
